//版本二
//数据抽象
const traffic = document.getElementById('traffic');

const stateList = [
    {state: 's1', last: 3000},   //红
    {state: 's2', last: 1000},   //黄
    {state: 's3', last: 3000}];  //绿

function start(traffic, stateList) {
    function applyState(stateIndex) {
        const {state, last} = stateList[stateIndex];
        traffic.className = state;
        setTimeout(() => {
            applyState((stateIndex + 1) % stateList.length);
        }, last)

    }

    applyState(0);
}

start(traffic, stateList);